<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商" prop="supname">
        <el-input
          v-model="queryParams.supname"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="storename">
        <el-input
          v-model="queryParams.storename"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属类别" prop="cname">
        <el-input
          v-model="queryParams.cname"
          placeholder="请输入所属类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brname">
        <el-input
          v-model="queryParams.brname"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名称" prop="drugname">
        <el-input
          v-model="queryParams.drugname"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['drug:drug:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['drug:drug:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['drug:drug:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['drug:drug:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drugList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="批准号" align="center" prop="drugnum" />
      <el-table-column label="药品名称" align="center" prop="drugname" />
      <el-table-column label="生产日期" align="center" prop="datemanufacture" />
      <el-table-column label="到期日期" align="center" prop="duedate" />
      <el-table-column label="计量" align="center" prop="drugdosage" />
      <el-table-column label="使用方式" align="center" prop="illustrate" />
      <el-table-column label="包装类型" align="center" prop="size" />
      <el-table-column label="是否处方" align="center" prop="prescription" />
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="使用人群" align="center" prop="usergroup" />
      <el-table-column label="存储方式" align="center" prop="storeconditions" />
      <el-table-column label="库存数量" align="center" prop="count" />
      <el-table-column label="供应商" align="center" prop="supname" />
      <el-table-column label="仓库名称" align="center" prop="storename" />
      <el-table-column label="所属类别" align="center" prop="cname" />
      <el-table-column label="品牌" align="center" prop="brname" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createtime" />
      <el-table-column label="修改时间" align="center" prop="modifytime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['drug:drug:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['drug:drug:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供应商" prop="supid">
          <el-select v-model="form.supid" placeholder="请输入供应商" >
            <el-option v-for="(item, index) in optionSuppliers" :key="index"
                       :label="item.supname" :value="item.supid" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库名称" prop="storid">
          <el-select v-model="form.storid" placeholder="请输入仓库名称" >
            <el-option v-for="(item,index) in optionStores" :key="index"
                       :label="item.storename" :value="item.storid" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属类别" prop="cid">
          <el-select v-model="form.cid" placeholder="请输入所属类别" >
            <el-option v-for="(item, index) in optionCategory" :key="index"
                       :label="item.cname" :value="item.cid" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属品牌" prop="brid">
          <el-select v-model="form.brid" placeholder="请输入品牌" >
            <el-option v-for="(item, index) in optionBrands" :key="index"
                       :label="item.brname" :value="item.brid" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="批准号" prop="drugnum">
          <el-input v-model="form.drugnum" placeholder="请输入批准号" />
        </el-form-item>
        <el-form-item label="药品名称" prop="drugname">
          <el-input v-model="form.drugname" placeholder="请输入药品名称" />
        </el-form-item>
        <el-form-item label="生产日期" prop="datemanufacture">
          <el-input v-model="form.datemanufacture" placeholder="请输入生产日期" />
        </el-form-item>
        <el-form-item label="到期日期" prop="duedate">
          <el-input v-model="form.duedate" placeholder="请输入到期日期" />
        </el-form-item>
        <el-form-item label="计量" prop="drugdosage">
          <el-input v-model="form.drugdosage" placeholder="请输入计量" />
        </el-form-item>
        <el-form-item label="使用方式" prop="illustrate">
          <el-input v-model="form.illustrate" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="包装类型" prop="size">
          <el-input v-model="form.size" placeholder="请输入包装类型" />
        </el-form-item>
        <el-form-item label="是否处方" prop="prescription">
          <el-select v-model="form.prescription" placeholder="请输入是否处方" >
            <el-option label="处方药" value="处方药"></el-option>
            <el-option label="非处方药" value="非处方药"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入生产厂家" />
        </el-form-item>
        <el-form-item label="使用人群" prop="usergroup">
          <el-select v-model="form.usergroup" placeholder="请输入使用人群" >
              <el-option label="成人" value="成人"></el-option>
              <el-option label="儿童" value="儿童"></el-option>
              <el-option label="老年人" value="老年人"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="存储方式" prop="storeconditions">
          <el-input v-model="form.storeconditions" placeholder="请输入存储方式" />
        </el-form-item>
        <el-form-item label="库存数量" prop="count">
          <el-input v-model="form.count" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDrug, getDrug, delDrug, addDrug, updateDrug } from "@/api/drug/drug";
import { getAllSuppliers } from "@/api/supplier/supplier";
import { getAllBrands } from "@/api/brand/brand";
import { getAllCategory } from "@/api/category/category";
import { getAllStores } from "@/api/store/store";

export default {
  name: "Drug",
  data() {
    return {
      optionSuppliers:[],
      optionBrands:[],
      optionCategory:[],
      optionStores:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品管理表格数据
      drugList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supid: null,
        storid: null,
        cid: null,
        brid: null,
        drugname: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supid: [
          { required: true, message: "供应商不能为空", trigger: "blur" }
        ],
        storid: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        cid: [
          { required: true, message: "所属类别不能为空", trigger: "blur" }
        ],
        brid: [
          { required: true, message: "品牌不能为空", trigger: "blur" }
        ],
        drugnum: [
          { required: true, message: "批准号不能为空", trigger: "blur" }
        ],
        drugname: [
          { required: true, message: "药品名称不能为空", trigger: "blur" }
        ],
        datemanufacture: [
          { required: true, message: "生产日期不能为空", trigger: "blur" }
        ],
        duedate: [
          { required: true, message: "到期日期不能为空", trigger: "blur" }
        ],
        drugdosage: [
          { required: true, message: "计量不能为空", trigger: "blur" }
        ],
        illustrate: [
          { required: true, message: "使用方式不能为空", trigger: "blur" }
        ],
        size: [
          { required: true, message: "包装类型不能为空", trigger: "blur" }
        ],
        prescription: [
          { required: true, message: "是否处方不能为空", trigger: "blur" }
        ],
        manufacturer: [
          { required: true, message: "生产厂家不能为空", trigger: "blur" }
        ],
        usergroup: [
          { required: true, message: "使用人群不能为空", trigger: "blur" }
        ],
        storeconditions: [
          { required: true, message: "存储方式不能为空", trigger: "blur" }
        ],
        count: [
          { required: true, message: "库存数量不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAllSupplier();
    this.getAllBrand();
    this.getAllCategories();
    this.getAllStore();
  },
  methods: {
    getAllStore(){
      getAllStores().then(res=>{
        this.optionStores=res.data;
      })
    },
    getAllCategories(){
      getAllCategory().then(res=>{
        this.optionCategory=res.data;
      })
    },
    getAllBrand(){
      getAllBrands().then(res=>{
        this.optionBrands=res.data;
      })
    },
    getAllSupplier(){
      getAllSuppliers().then(res=>{
        this.optionSuppliers=res.data;
      })
    },
    /** 查询药品管理列表 */
    getList() {
      this.loading = true;
      listDrug(this.queryParams).then(response => {
        this.drugList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        drugid: null,
        supid: null,
        storid: null,
        cid: null,
        brid: null,
        drugnum: null,
        drugname: null,
        datemanufacture: null,
        duedate: null,
        drugdosage: null,
        illustrate: null,
        size: null,
        prescription: null,
        manufacturer: null,
        usergroup: null,
        storeconditions: null,
        count: null,
        remark: null,
        createtime: null,
        modifytime: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.drugid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const drugid = row.drugid || this.ids
      getDrug(drugid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.drugid != null) {
            updateDrug(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrug(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const drugids = row.drugid || this.ids;
      this.$modal.confirm('是否确认删除药品管理编号为"' + drugids + '"的数据项？').then(function() {
        return delDrug(drugids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('drug/drug/export', {
        ...this.queryParams
      }, `drug_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
